#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QString>
#include <QComboBox>
#include <QFontMetrics>
#include <QSpacerItem>
#include <QPainter>
class SettingsWidget:public QWidget{
public:
    SettingsWidget(QWidget *parent = nullptr):QWidget(parent)
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->setContentsMargins(20,20,20,20);//设置边距
        mainLayout->setSpacing(10);

        //标题1设置字体加粗
        QLabel *titleLabel1= new QLabel("通用");
        titleLabel1->setStyleSheet("font-size:14px; font-weight:hold");
        titleLabel1->setContentsMargins(15,0,15,0);//设置边距
        titleLabel1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        mainLayout->addWidget(titleLabel1);

        // mainLayout->addSpacing(10);  // 基础间距
        // mainLayout->addSpacerItem(
        //     new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)
        // );  // 设置弹簧


        //添加两个有按钮的设置
        addSetItem(mainLayout,"睡眠/休眠后唤醒需要密码");
        addSetItem(mainLayout,"唤醒屏幕时需要密码");


        //添加带有下拉菜单的部分
        addSetCombo(mainLayout,"按电源键时执行", {"询问","关机","睡眠","休眠"});
        addSetCombo(mainLayout,"此时间段后关闭显示器", {"从不","两小时","一小时","30分钟","15分钟","10分钟","5分钟","1分钟"});
        addSetCombo(mainLayout,"此时间段后系统进入睡眠",{"从不","两小时","一小时","30分钟"});
        addSetCombo(mainLayout,"笔记本合盖时执行",{"休眠","睡眠","关机","关闭显示器","无操作"});

        //mainLayout->setSpacing(10);
        addSetItem(mainLayout,"资源动态调度");
        // mainLayout->addSpacing(10);
        // mainLayout->addSpacerItem(
        //     new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)
        // );


        //电源计划
        QLabel *titleLabel2 = new QLabel("电源计划");
        titleLabel2->setStyleSheet("font-size:14px; font-weight:hold");
        titleLabel2->setContentsMargins(15,0,15,0);//设置边距
        titleLabel2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        mainLayout->addWidget(titleLabel2);
        // mainLayout->setSpacing(0);

        // mainLayout->addSpacing(10);  // 基础间距
        // mainLayout->addSpacerItem(
        //     new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)
        // );  // 设置弹簧

        addSetCombo(mainLayout,"使用电源时", {"最佳性能","最佳能效","平衡"});
        addSetCombo(mainLayout,"使用电池时", {"最佳性能","最佳能效","平衡"});
        // mainLayout->addSpacing(10);  // 基础间距
        // mainLayout->addSpacerItem(
        //     new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));


        //电池节能计划
        QLabel *titleLabel3 = new QLabel("电池节能计划");
        titleLabel3->setStyleSheet("font-size:14px; font-weight:hold");
        titleLabel3->setContentsMargins(15,0,15,0);//设置边距
        titleLabel3->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        mainLayout->setSpacing(0);
        mainLayout->addWidget(titleLabel3);

        // mainLayout->addSpacing(10);  // 基础间距
        // mainLayout->addSpacerItem(
        //     new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)
        // );  // 设置弹簧

        addSetdoubleCombo(mainLayout);
        addSetCombo(mainLayout,"低电量时通知", {"40%","30%","20%","10%"});
        addSetItem(mainLayout,"低电量时自动开启节能模式");
        // mainLayout->addSpacing(10);  // 基础间距
        // mainLayout->addSpacerItem(
        // new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));

        mainLayout->addStretch();//添加伸缩空间，自动调节
        setStyleSheet("background-color:#f0f0f0;");
    }
private:
    void addSetItem(QVBoxLayout *layout, const QString &text)
    {
        QFrame *itemFrame =new QFrame();
        itemFrame->setStyleSheet("background-color: white");
        itemFrame->setFixedHeight(40);
        //水平布局
        QHBoxLayout *itemLayout = new QHBoxLayout(itemFrame);
        itemLayout->setContentsMargins(15,0,15,0);
        itemLayout->setSpacing(0);
        //文本标签
        QLabel *textLabel =new QLabel(text);
        textLabel->setStyleSheet("font-size:14px");
        //按钮
        QPushButton *slideButton = new QPushButton();
        slideButton->setCheckable(true);
        slideButton->setFixedSize(50,30);
        //slideButton->setStyleSheet("QPushButton {border-radius: 15px; background-color: #e0e0e0;}"
                                  // "QPushButton:checked{background-color:#87CEEB}");
        slideButton->setCursor(Qt::PointingHandCursor);
        slideButton->setStyleSheet(
                    "QPushButton {"
                    "  border: none;"
                    "  border-radius: 15px;"
                    "  background-color: #e0e0e0;"
                    "  padding: 0px;"
                    "}"
                    "QPushButton:checked {"
                    "  background-color: #87CEEB;"
                    "}"
                    "QPushButton::before {"
                    "  content: '';"
                    "}"
                    "QPushButton::after {"
                    "  content: '';"
                    "}"
                    "QPushButton::indicator { width:0; height:0; }"
                    "QPushButton::checked { }"
                    "QPushButton::unchecked { }"
                );

                // 使用事件过滤器自定义绘制滑块小圆点
                class SlideButtonEventFilter : public QObject {
                public:
                    QPushButton *btn;
                    SlideButtonEventFilter(QPushButton *b) : btn(b) {}
                protected://重写eventFilter，只处理Paint事件
                    bool eventFilter(QObject *obj, QEvent *event) override {
                        if (event->type() == QEvent::Paint) {//自定义绘制逻辑
                            QStyleOptionButton opt;
                            opt.initFrom(btn);
                            QPainter p(btn);
                            // 背景
                            QColor bg = btn->isChecked() ? QColor("#87CEEB") : QColor("#e0e0e0");
                            p.setRenderHint(QPainter::Antialiasing);//抗锯齿
                            p.setBrush(bg);//画刷颜色
                            p.setPen(Qt::NoPen);//无边框
                            p.drawRoundedRect(btn->rect(), 15, 15);//绘制圆角矩形
                            // 小圆
                            int margin = 3;
                            int d = btn->height() - 2*margin;
                            int x = btn->isChecked() ? btn->width() - d - margin : margin;
                            QRect circleRect(x, margin, d, d);
                            p.setBrush(Qt::white);
                            p.drawEllipse(circleRect);//绘制原点
                            return true;//事件已处理 ，不在继续传递
                        }
                        return QObject::eventFilter(obj, event);
                    }
                };
        slideButton->installEventFilter(new SlideButtonEventFilter(slideButton));

        //添加到布局
        itemLayout->addWidget(textLabel);
        itemLayout->addStretch();//伸缩空间，自动调节
        itemLayout->addWidget(slideButton);
        //添加到主布局
        layout->addWidget(itemFrame);
        //layout->addSpacing(0);


    }
    void addSetCombo(QVBoxLayout *layout, const QString &text, const QStringList &items)
    {
        QFrame *itemFrame =new QFrame();
        itemFrame->setStyleSheet("background-color: white");
        itemFrame->setFixedHeight(40);
        //水平布局
        QHBoxLayout *itemLayout = new QHBoxLayout(itemFrame);
        itemLayout->setContentsMargins(15,0,15,0);
        itemLayout->setSpacing(0);

        QLabel *textLabel =new QLabel(text);
        textLabel->setStyleSheet("font-size:14px");

        QComboBox *comboBox = new QComboBox();
        comboBox->addItems(items);
        comboBox->setCurrentIndex(0);
        comboBox->setEditable(true);
        comboBox->setStyleSheet("QComboBox{padding: 5px;combobox-popup: 0; min-width:100px;background-color:white; border-radius:5; border: 1px solid #ccc;}"
                                "QComboBox::drop-down{subcontrol-origin: padding; subcontrol-position: right center; width: 20px;}"
                                "QComboBox QAbstractItemView {border: 1px solid #ccc;background: white;selection-background-color: #0078d7;selection-color:white;}"
                                "QComboBox QAbstractItemView::item:hover{background-color:#e5f1fb;}");
        comboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
        itemLayout->addWidget(textLabel);
        itemLayout->addStretch();
        itemLayout->addWidget(comboBox);

        layout->addWidget(itemFrame);
        //layout->addSpacing(0);
    }
    void addSetdoubleCombo(QVBoxLayout *layout)
    {
        QFrame *itemFrame =new QFrame();
        itemFrame->setStyleSheet("background-color: white");
        itemFrame->setFixedHeight(40);
        //水平布局
        QHBoxLayout *itemLayout = new QHBoxLayout(itemFrame);
        itemLayout->setContentsMargins(15,0,15,0);
        itemLayout->setSpacing(0);

        QLabel *textLabel1 =new QLabel("电量低于");
        textLabel1->setStyleSheet("font-size:14px");

        QComboBox *comboBox1 = new QComboBox();
        comboBox1->addItems({"9%","7%","5%","3%","1%"});
        comboBox1->setCurrentIndex(0);
        comboBox1->setEditable(true);

        comboBox1->setStyleSheet("QComboBox{padding: 5px;combobox-popup: 0; min-width:100px;background-color:white; border-radius:5; border: 1px solid #ccc;}"
                                "QComboBox::drop-down{subcontrol-origin: padding; subcontrol-position: right center; width: 20px;}"
                                "QComboBox QAbstractItemView {border: 1px solid #ccc;background: white;selection-background-color: #0078d7;selection-color:white;}"
                                "QComboBox QAbstractItemView::item:hover{background-color:#e5f1fb;}");

        QLabel *textLabel2 =new QLabel("时执行");
        textLabel2->setStyleSheet("font-size:14px");

        QComboBox *comboBox2 = new QComboBox();
        comboBox2->addItems({"休眠","睡眠","关机","关闭显示器","无操作"});
        comboBox2->setCurrentIndex(0);
        comboBox2->setEditable(true);
        comboBox2->setContentsMargins(500,0,10,0);
        comboBox2->setStyleSheet("QComboBox{padding: 5px;combobox-popup: 0; min-width:100px;background-color:white; border-radius:5; border: 1px solid #ccc;}"
                                "QComboBox::drop-down{subcontrol-origin: padding; subcontrol-position: right center; width: 20px;}"
                                "QComboBox QAbstractItemView {border: 1px solid #ccc;background: white;selection-background-color: #0078d7;selection-color:white;}"
                                "QComboBox QAbstractItemView::item:hover{background-color:#e5f1fb;}");

        itemLayout->addWidget(textLabel1);
        itemLayout->addWidget(comboBox1);
        itemLayout->addWidget(textLabel2);
        // 先添加固定间距，再保留弹性空间
        itemLayout->addSpacing(10);  // 基础间距
        itemLayout->addSpacerItem(
            new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)
        );  // 设置弹簧
        itemLayout->addStretch();
        itemLayout->addWidget(comboBox2);
        //itemLayout->setSpacing(10);
        layout->addWidget(itemFrame);

    }
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow) //初始化列表构造
{
    ui->setupUi(this);
    QWidget *centralWidget = new QWidget(this);

    setCentralWidget(centralWidget);


    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);//垂直布局管理器

    QLineEdit *searchBox = new QLineEdit(this);
    searchBox->setPlaceholderText("搜素");
    searchBox->setContentsMargins(300,5,300,5);
    mainLayout->addWidget(searchBox);
    mainLayout->setSpacing(0);

    // 新增：用QScrollArea包裹SettingsWidget
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    SettingsWidget *settingsWidget = new SettingsWidget(this);
    //mainLayout->addWidget(settingsWidget);
    scrollArea->setWidget(settingsWidget);
    mainLayout->addWidget(scrollArea);
}

MainWindow::~MainWindow()
{
    delete ui;
}



